using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MainMenu_ContextMenu : MonoBehaviour
{
	[SerializeField]
	private bool MenuOpen;

	[SerializeField]
	private bool AllowInput;

	[SerializeField]
	private bool WaitingUntilZUnpressed;

	[SerializeField]
	private Animator ContextMenuAnimator;

	[SerializeField]
	private MainMenu_SectionManager PreviousSection;

	[SerializeField]
	private TRIG_LEVELTRANSITION LevelTransition;

	[SerializeField]
	private TextMeshProUGUI ContextText;

	[SerializeField]
	private TextMeshProUGUI ContextTitleText;

	[SerializeField]
	private TextMeshProUGUI ControlsText;

	[SerializeField]
	private List<GameObject> ContextGoals = new List<GameObject>();

	[SerializeField]
	private GameObject InputControlText;

	[SerializeField]
	private Image ContextIcon;

	[SerializeField]
	private AudioSource ContextSource;

	[SerializeField]
	private AudioClip Sound_Confirm;

	[SerializeField]
	private AudioClip Sound_Return;

	[SerializeField]
	private ChatboxSimpleText GonerText;

	[SerializeField]
	private CHATBOXTEXT ChatboxText;

	[SerializeField]
	private bool RanText;

	private Hypothesis CurrentHypothesis;

	public void OpenContextMenu(Hypothesis Hypothesis, MainMenu_SectionManager thisSection)
	{
		if ((Object)(object)Hypothesis != (Object)null && (Object)(object)thisSection != (Object)null)
		{
			CurrentHypothesis = Hypothesis;
			PreviousSection = thisSection;
			MenuOpen = true;
			if (Input.GetKey((KeyCode)122) || Input.GetKey((KeyCode)13))
			{
				WaitingUntilZUnpressed = true;
			}
			else
			{
				((MonoBehaviour)this).StartCoroutine(OpenMenuInputDebounce());
			}
			if ((Object)(object)ChatboxText != (Object)null && (Object)(object)GonerText != (Object)null && !RanText)
			{
				RanText = true;
				GonerText.RunText(ChatboxText, 0);
			}
			InputControlText.SetActive(true);
			((TMP_Text)ContextText).text = CurrentHypothesis.HypothesisDescription;
			((TMP_Text)ContextTitleText).text = CurrentHypothesis.HypothesisName;
			ContextIcon.sprite = CurrentHypothesis.HypothesisMenuSprite;
			SetupCurrentHypothesisGoals();
			((TMP_Text)ControlsText).text = "";
			ContextMenuAnimator.Play("ContextMenu_FadeIn");
		}
		else
		{
			Debug.LogWarning((object)"Hypothesis or thisSection is null?");
			Debug.LogWarning((object)Hypothesis);
			Debug.LogWarning((object)thisSection);
			MenuOpen = false;
		}
	}

	public void CloseContextMenu()
	{
		MenuOpen = false;
		RanText = false;
		ContextMenuAnimator.Play("ContextMenu_FadeOut");
		PreviousSection.SelectionState = 1;
	}

	public void CloseContextMenu_PreventPreviousStateUpdate()
	{
		MenuOpen = false;
		RanText = false;
		ContextMenuAnimator.Play("ContextMenu_Hidden");
	}

	private void Update()
	{
		if ((Input.GetKeyDown((KeyCode)120) || Input.GetKeyDown((KeyCode)303)) && MenuOpen && AllowInput)
		{
			((MonoBehaviour)this).StartCoroutine(CloseContextDebounce());
			ContextSource.PlayOneShot(Sound_Return);
		}
		if ((Input.GetKeyDown((KeyCode)122) || Input.GetKeyDown((KeyCode)13)) && MenuOpen && AllowInput && !WaitingUntilZUnpressed)
		{
			AllowInput = false;
			InputControlText.SetActive(false);
			LevelTransition.LevelToGo = CurrentHypothesis.HypothesisStartingScene;
			LevelTransition.BeginTransition(2f);
			MusicManager.StopSong(Fade: true, 1f);
			ContextSource.PlayOneShot(Sound_Confirm);
		}
		if (Input.GetKeyUp((KeyCode)122) || Input.GetKeyUp((KeyCode)13))
		{
			WaitingUntilZUnpressed = false;
		}
		if (Input.GetKeyDown((KeyCode)127) && MenuOpen && AllowInput)
		{
			PlayerPrefs.SetInt("HypothesisGoal_" + CurrentHypothesis.HypothesisGoals[1].GoalPlayprefName, 0);
		}
	}

	private IEnumerator CloseContextDebounce()
	{
		yield return (object)new WaitForSeconds(0.05f);
		CloseContextMenu();
	}

	private IEnumerator OpenMenuInputDebounce()
	{
		yield return (object)new WaitForSeconds(0.05f);
		AllowInput = true;
	}

	private void SetupCurrentHypothesisGoals()
	{
		for (int i = 0; i < ContextGoals.Count; i++)
		{
			if (i < CurrentHypothesis.HypothesisGoals.Length)
			{
				EditGoalValues(i, CurrentHypothesis.HypothesisGoals[i]);
			}
			else
			{
				ClearGoalValue(i);
			}
		}
	}

	private void EditGoalValues(int index, HypothesisGoal Goal)
	{
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = ContextGoals[index];
		GameObject val2 = null;
		GameObject val3 = null;
		for (int i = 0; i < val.transform.childCount; i++)
		{
			if (((Object)val.transform.GetChild(i)).name == "GoalHint")
			{
				val2 = ((Component)val.transform.GetChild(i)).gameObject;
			}
			if (((Object)val.transform.GetChild(i)).name == "GoalProgress")
			{
				val3 = ((Component)val.transform.GetChild(i)).gameObject;
			}
		}
		TextMeshProUGUI component = val2.GetComponent<TextMeshProUGUI>();
		TextMeshProUGUI component2 = val3.GetComponent<TextMeshProUGUI>();
		((TMP_Text)component).text = Goal.GoalHint;
		int num = PlayerPrefs.GetInt("HypothesisGoal_" + Goal.GoalPlayprefName, 0);
		if (num >= Goal.RequiredTasks)
		{
			((Graphic)component).color = Color.green;
			((Graphic)component2).color = Color.green;
		}
		else
		{
			((Graphic)component).color = Color.white;
			((Graphic)component2).color = Color.white;
		}
		if (Goal.RequiredTasks > 1)
		{
			((TMP_Text)component2).text = num + "/" + Goal.RequiredTasks;
		}
		else if (num == 0)
		{
			((TMP_Text)component2).text = "( )";
		}
		else
		{
			((TMP_Text)component2).text = "(X)";
		}
	}

	private void ClearGoalValue(int index)
	{
		GameObject val = ContextGoals[index];
		GameObject val2 = null;
		GameObject val3 = null;
		for (int i = 0; i < val.transform.childCount; i++)
		{
			if (((Object)val.transform.GetChild(i)).name == "GoalHint")
			{
				val2 = ((Component)val.transform.GetChild(i)).gameObject;
			}
			if (((Object)val.transform.GetChild(i)).name == "GoalProgress")
			{
				val3 = ((Component)val.transform.GetChild(i)).gameObject;
			}
		}
		TextMeshProUGUI component = val2.GetComponent<TextMeshProUGUI>();
		TextMeshProUGUI component2 = val3.GetComponent<TextMeshProUGUI>();
		((TMP_Text)component).text = "";
		((TMP_Text)component2).text = "";
	}
}
